package com.hankol.newretail.adapter

import android.app.Activity
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.Constants
import com.baselibrary.extension.*
import com.baselibrary.utils.*
import com.baselibrary.view.RoundCornerImageView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.MallOrderShopDtoBean
import com.hankol.newretail.bean.RemarkBean
import com.hankol.newretail.utils.RetailPopUtils

/**确认订单   单个商品/多个商品
 * @author: merbng
 * @time: 2021/9/29 15:15
 * @description:
 */
class ConfrimOrderGoodListMultAdapter(data: List<MallOrderShopDtoBean>) : BaseMultiItemQuickAdapter<MallOrderShopDtoBean, BaseViewHolder>(data) {
    private var mAct: Activity? = null

    fun setCurrentActivity(activity: Activity) {
        this.mAct = activity
    }

    interface GoodClickCallBack {
        fun goodClick(productOrderId: Long);
    }

    lateinit var callback: GoodClickCallBack
    fun setCallBack(callback: GoodClickCallBack) {
        this.callback = callback
    }

    init {
        addItemType(Constants.TYPE_CONFIRM_ORDER_GOOD_SINGLE, R.layout.item_confirm_order_good_single)
        addItemType(Constants.TYPE_CONFIRM_ORDER_GOOD_MORE, R.layout.item_confirm_order_good_more)
    }

    override fun convert(helper: BaseViewHolder, item: MallOrderShopDtoBean) {
        helper.apply {
            setText(R.id.tv_winery_name, item.shopName.checkValue())
            val goodsList = item.mallOrderSkuDtoList
            getView<EditText>(R.id.et_single_remark).addTextChangedListenerClosure(afterTextChanged = {
                val remarkBean = RemarkBean(item.shopId, it.toString())
                item.remarkStr = CommonUtil.beanToJson(remarkBean)
            })
            when (item.itemType) {
                Constants.TYPE_CONFIRM_ORDER_GOOD_MORE -> {//多件商品
                    //如果单个店铺，隐藏备注
                    setGone(R.id.ll_mult_remark, item.mallOrderSkuDtoList.size != 0&&data.size>1)
                    val rvMultiGoods = getView<RecyclerView>(R.id.rv_multi_goods)
                    val multCoverAdapter = MultiGoodsCoverAdapter()
                    rvMultiGoods.adapter = multCoverAdapter
                    multCoverAdapter.setNewData(goodsList)
                    val tvMoreCnt = getView<TextView>(R.id.tv_more_cnt)
                    tvMoreCnt.setMallPriceTTF(mContext)
                    tvMoreCnt.buildSpannableString {
                        addText("¥${AmountUtil.changeF2YKeepTwo("${item.priceSum}")}") {
                            setColor("#333333")
                        }
                        addText("\n共") {
                            setColor("#666666")
                        }
                        addText("${item.numSum}") {
                            setColor("#333333")
                        }
                        addText("件") {
                            setColor("#666666")
                        }
                    }
                    tvMoreCnt.setOnClickListener {
                        mAct?.let {
                            RetailPopUtils.showGoodListingDialog(it, goodsList)
                        }
                    }
                    setGone(R.id.ll_goods, goodsList.size > 0)
                    //是否有不支持的商品
                    item.notSupportSkuDtoList?.let {
                        setGone(R.id.rl_no_support, it.checkNotNull())
                        val rvNoSupport = getView<RecyclerView>(R.id.rv_no_support)
                        val noSupportAdapter = NoBuyGoodsCoverAdapter()
                        rvNoSupport.adapter = noSupportAdapter
                        noSupportAdapter.setNewData(it)
                        setText(R.id.tv_no_buy_num, "以下商品(共${it.size}件)无法购买")
                        //查看原因
                        getView<TextView>(R.id.tv_see_reason).setOnClickListener { RetailPopUtils.showSeeReasonDialog(mAct!!, item.notSupportSkuDtoList) }
                        noSupportAdapter.setOnItemClickListener { adapter, view, position ->
                            RetailPopUtils.showSeeReasonDialog(mAct!!, item.notSupportSkuDtoList)
                        }
                    }
                }
                Constants.TYPE_CONFIRM_ORDER_GOOD_SINGLE -> {//单件商品
                    if (goodsList.size > 0) {
                        setGone(R.id.rl_one_good, true)
                        goodsList[0].let {
                            setText(R.id.tv_name, it.spuName.checkValue())
                            setText(R.id.tv_price, AmountUtil.changeF2YKeepTwo("${it.price}"))
                            setText(R.id.tv_spec, SpecUtils.getNormsStr(it.specList))
                            setText(R.id.tv_num, "x${it.num}")
                            //允许酒币支付：0否，1是
                            setGone(R.id.ll_is_coin_pay, it.scorePayPower == 1)
                            GlideUtils.loadImg_101(mContext, it.skuCover.checkValue(), helper.getView<RoundCornerImageView>(R.id.img_cover))
                            if (!it.mallProductGiftCommodityVos.isNullOrEmpty()){
                                setGone(R.id.ll_gift,true)
                                val goodsGiftConfirmOrderAdapter = GoodsGiftConfirmOrderAdapter()
                                helper.getView<RecyclerView>(R.id.rv_gift_list).adapter = goodsGiftConfirmOrderAdapter
                                goodsGiftConfirmOrderAdapter.setNewData(it.mallProductGiftCommodityVos)
                            } else{
                                setGone(R.id.ll_gift,false)
                            }
                        }
                        val tvPrice = helper.getView<TextView>(R.id.tv_price)
                        tvPrice.setMallPriceTTF(mContext)
                        helper.getView<TextView>(R.id.tv_unit).setMallPriceTTF(mContext)
                        getView<LinearLayout>(R.id.ll_is_coin_pay).setOnClickListener {
                            val dictionary = SpUtils.getInstance().loadDictionaryrBean
                            val desc = dictionary.getByName_system("currencyRules")
                            RetailPopUtils.goodsServerPop(mAct!!, null, desc, "酒币规则")
                        }
                    }
                    //是否有不支持的商品
                    item.notSupportSkuDtoList?.let {
                        setGone(R.id.rl_no_support, it.size > 0)
                        //多个店铺时，使用内部布局 的备注，
                        if (item.notSupportSkuDtoList.size > 0) {
                            setGone(R.id.ll_inner_remark, item.mallOrderSkuDtoList.size != 0&&data.size>1)
                        }else{
                            setGone(R.id.ll_inner_remark, data.size>1)
                        }
                        if (it.size > 0) {
                            setGone(R.id.ll_is_coin_pay, false)
                            setGone(R.id.rl_one_good, goodsList.size != 0)
                            val rvNoSupport = getView<RecyclerView>(R.id.rv_no_support)
                            val noSupportAdapter = NoBuyGoodsCoverAdapter()
                            rvNoSupport.adapter = noSupportAdapter
                            noSupportAdapter.setNewData(it)
                            setText(R.id.tv_no_buy_num, "以下商品(共${it.size}件)无法购买")
                            //查看原因
                            getView<TextView>(R.id.tv_see_reason).setOnClickListener { RetailPopUtils.showSeeReasonDialog(mAct!!, item.notSupportSkuDtoList) }
                            noSupportAdapter.setOnItemClickListener { adapter, view, position ->
                                RetailPopUtils.showSeeReasonDialog(mAct!!, item.notSupportSkuDtoList)
                            }
                        }
                    }
                }
            }
        }
    }
}